//
//  UIImage+WebImage.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/8/23.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import SDWebImage

extension UIImageView {
    func zn_setImage(urlString: String?, placeholderImage: UIImage?, isAvatar: Bool = false) {
        
        guard let urlString = urlString,
              let url = URL(string: urlString)
        else {
            image = placeholderImage
            
            return
        }
        
        //可选项只作用于Swift，OC中对nil发送消息是合法的
        sd_setImage(with: url,
                    placeholderImage: placeholderImage,
                    options: [],
                    progress: nil) {
                        // 防止出现循环引用
                        [weak self] (image, _, _, _) in
            if isAvatar {
                // 画出圆角
                self?.image = image?.cz_avatarImage(size: self?.bounds.size)
            }
        }
    }
}
